package com.supermap.originTest;


import com.supermap.data.*;

import java.util.ArrayList;


public class Test0906 {

    /**
     * 程序入口点
     * The entrance of the programme.
     */
    public static void main(String[] args) {

		
		nmQueryTest();
		


	}

	static void nmQueryTest(){
		Workspace workspace = new Workspace();
		long t1,t2,t3;
		String fieldValue = "DF5";
		DatasourceConnectionInfo info = new DatasourceConnectionInfo();
		info.setServer("172.16.13.60:5432");
		info.setEngineType(EngineType.POSTGRESQL);
		info.setDatabase("pg1");
		info.setUser("postgres");
		info.setPassword("123456");
		info.setMaxConnPoolNum(50);
		info.setAlias("pg");
		Datasource datasource = workspace.getDatasources().open(info);
		DatasetVector dataset1 = (DatasetVector) datasource.getDatasets().get(16);

		FieldInfos fieldInfos = dataset1.getFieldInfos();
		ArrayList fieldsArray = new ArrayList();
		StringBuilder filterBuilder = new StringBuilder();
		t1 = System.currentTimeMillis();
		System.out.println("start time : "+System.currentTimeMillis());
		for (int count = 0; count < fieldInfos.getCount(); count++) {
			FieldInfo fieldInfo = fieldInfos.get(count);
			FieldType fieldType = fieldInfo.getType();
			if(!(fieldInfo.getName().equalsIgnoreCase("geometry") || fieldInfo.getName().startsWith("SM")
					|| fieldInfo.getName().startsWith("Sm")|| fieldInfo.getName().startsWith("sM")|| fieldInfo.getName().startsWith("sm")||
					fieldInfo.getName().equalsIgnoreCase("id")) &&
					(fieldType == FieldType.CHAR || fieldType == FieldType.TEXT || fieldType == FieldType.WTEXT)){
				fieldsArray.add(fieldInfo.getName());
				if (filterBuilder.length() > 0) {
					filterBuilder.append(" OR ");
				}
				filterBuilder.append(fieldInfo.getName())
						.append(" LIKE ")
						.append("'%" + fieldValue  + "%'");

			}
		}
		t2 = System.currentTimeMillis();
		System.out.println("build attribute time : "+System.currentTimeMillis());
		QueryParameter queryParameter = new QueryParameter();
		queryParameter.setCursorType(CursorType.STATIC);
		queryParameter.setAttributeFilter(String.valueOf(filterBuilder));
		queryParameter.setSpatialQueryMode(SpatialQueryMode.NONE);
		Recordset recordset = dataset1.query(queryParameter);
		t3 = System.currentTimeMillis();
		System.out.println("query end time : "+System.currentTimeMillis());

		System.out.println("construct spent : "+(t2-t1));
		System.out.println("query spent : "+(t3-t2));
	}


}  

